package org.eenie.appseed.ui.caution


import android.app.Notification
import android.app.PendingIntent
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.NotificationUtils
import com.blankj.utilcode.util.VibrateUtils
import com.mikhaellopez.rxanimation.shake

import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.eenie.appseed.R
import org.eenie.appseed.databinding.ActivityCautionBinding
import org.eenie.core.base.viewbinding.BaseBindingActivity
import org.eenie.core.ext.onClick



class CautionActivity : BaseBindingActivity<ActivityCautionBinding>() {


    val hints = arrayOf("打卡了没有!!", "打了 ^.^ 嘿嘿嘿~~~", "忘了 T.T 嘤嘤嘤...")

    var isPrinting = false

    override fun initUI() {
        this.binding.btnHappy.onClick {
            this@CautionActivity.binding.viewAnim.fireState("State Machine 1", "Correct")
            printText(hints[1], 1)
        }
        this.binding.btnSad.onClick {
            this@CautionActivity.binding.viewAnim.fireState("State Machine 1", "Incorrect")
            printText(hints[2], 0)
        }
        this.binding.viewAnim.fireState("State Machine 1", "Incorrect")


        this.binding.btnCard.onClick {
            if (this.tag == 0) {
                openRimet()
                finish()
            } else {
                finish()
            }
        }


        hint()

        notice()

    }


    fun notice() {
        NotificationUtils.notify(1000) { param ->
            param.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("打卡提醒")
                .setContentText("打卡了没有!!")
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_VIBRATE or Notification.DEFAULT_ALL or Notification.DEFAULT_SOUND)
                .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_MUTABLE))
                .setAutoCancel(true)
        }

    }


    private fun hint() {
        printText(hints[0], -1)
        VibrateUtils.vibrate(longArrayOf(100, 200, 100, 200), 2)
    }

    private fun openRimet() {
        AppUtils.launchApp("com.alibaba.android.rimet")
    }

    override fun providerBinding() = ActivityCautionBinding.inflate(layoutInflater)

    override fun onDestroy() {
        super.onDestroy()
        VibrateUtils.cancel()
    }


    private fun printText(text: String, state: Int) {
        VibrateUtils.cancel()
        binding.btnCard.tag = state
        lifecycleScope.launch {
            if (!isPrinting) {
                isPrinting = true
                binding.btnCard.visibility = View.GONE
                binding.tvHint.text = ""
                for (item in text) {
                    binding.tvHint.append(item.toString())
                    delay(100)
                }
                binding.btnCard.visibility = when (state) {

                    -1 -> {
                        View.GONE
                    }

                    0 -> {
                        binding.btnCard.text = "去打卡"
                        binding.btnCard.isSelected = false
                        binding.btnCard.shake()
                            .subscribe()
                        View.VISIBLE

                    }

                    1 -> {
                        binding.btnCard.text = "回家家"
                        binding.btnCard.isSelected = true
                        binding.btnCard.shake()
                            .subscribe()
                        View.VISIBLE
                    }

                    else -> {
                        View.GONE
                    }
                }
                isPrinting = false
            }

        }
    }
}